//
//  CouponViewController.m
//  Doctors
//
//  Created by 王森 on 16/4/13.
//  Copyright © 2016年 王森. All rights reserved.
//

#import "CouponViewController.h"
#import "CouponTableViewCell.h"
#import "YouHuiQuanModel.h"
#import "KLCPopup.h"

@interface CouponViewController ()
{
    KLCPopup* popup;
    ;
    
}
@end

@implementation CouponViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTextTitleViewWithFrame:CGRectMake(0, 0, 60, 20) title:@"我的优惠券" fontSize:18];
    [self customTableView];
    //    self.tableview.frame=CGRectMake(0, slideControl.bottom, SCREEN_WIDTH, ScreenHeight-64-48-48);
    //
    
    [self addLeftBackBtn:nil];
    
    [self requestData];
    
    UINib *nib=[UIView loadNibNamed:@"CouponTableViewCell" bundle:nil];
    
    [self.tableview registerNib:nib forCellReuseIdentifier:@"CouponTableViewCell"];
    // Do any additional setup after loading the view.

    // Do any additional setup after loading the view.
}
-(void)requestData
{
    
    
    [self addProgressHUD];
    
    NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys: [self getArtisanId],@"artisanId",@"1",@"page",@"100",@"size",  nil];
    NSLog(@"订单详情参数：%@",dictionary);
    [[WSNetworkApi shareManager]postWithUrl:@"/artisan/queryCouponList" parameters:dictionary option:RequestCenterCachePolicyNormal sucess:^(id responseObject) {
        
        [self removeStatuslable];
        
        NSLog(@"%@",responseObject);
        
        if ([responseObject[@"status"] isEqualToString:@"ok"]) {
            
            NSArray *array=responseObject[@"couponList"];
            for (NSDictionary *dic in array) {
                YouHuiQuanModel *model=[[YouHuiQuanModel alloc]initWithDic:dic];
                [self.dataArray addObject:model];
                
                
            }
            
            
        }
        else{
            
            [self showFailed:responseObject[@"message"]];
            
        }
        
        [self.tableview reloadData];
        
        
    } failur:^(NSError *error) {
        
        [self removeStatuslable];

    }];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    __weak typeof(self)weakSelf=self;
    //
    //    OrderModel *model=self.dataArray[indexPath.row];
    
    CouponTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CouponTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    [cell cellFuzhi:self.dataArray[indexPath.row]];
    
   
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    UIImageView *img=[[UIImageView alloc]initWithImage:IMAGE(@"pic_person")];
    
    YouHuiQuanModel *modle=self.dataArray[indexPath.row];
    
    UIImageView *img=[[UIImageView alloc]initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:modle.QRcode]]]];
                      
    FRAME(img, 0, 0, 150, 150);
    
//    [self.view makeToast:@"二维码" duration:1 position:nil title:nil image:@"" style:@"CSToastPositionTop" completion:^(BOOL didTap) {
//        
//    }:img];
    
    
    KLCPopupLayout layout = KLCPopupLayoutMake((KLCPopupHorizontalLayout)KLCPopupHorizontalLayoutCenter,
                                               (KLCPopupVerticalLayout)KLCPopupVerticalLayoutCenter);
    popup = [KLCPopup popupWithContentView:img
                                  showType:KLCPopupShowTypeBounceIn
                               dismissType:KLCPopupDismissTypeBounceOut
                                  maskType:KLCPopupMaskTypeDimmed
                  dismissOnBackgroundTouch:YES
                     dismissOnContentTouch:NO];
    
    
    [popup showWithLayout:layout duration:250];

    [popup show];
    
    
    
//
//    [self.navigationController.view makeToast:@""
//                                     duration:30
//                                     position:CSToastPositionCenter
//                                        title:@""
//                                        image:img.image
//                                        style:nil
//                                   completion:^(BOOL didTap) {
//                                       if (didTap) {
//                                           NSLog(@"completion from tap");
//                                       } else {
//                                           NSLog(@"completion without tap");
//                                       }
//                                   }];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.dataArray.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
            return 163;;
  }




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
